package com.kimstore.utility;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * Reads and writes form the configuration properties
 *
 * @author wednesday
 *
 */
public class ConfigurationPropertiesUtil {

    private static String PROPERTIES_URL = "configuration/properties.properties";
    private static Properties properties;

    public static Properties loadProperties() throws IOException {
        Properties properties = new Properties();
        FileInputStream fis = null;

        fis = new FileInputStream(PROPERTIES_URL);
        properties.load(fis);
        fis.close();

        return properties;
    }

    public static String getProperty(String key) {
        if (properties == null) {
            try {
                properties = loadProperties();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return properties.getProperty(key);
    }

    public static void saveProperty(String key, String property) {
        properties.setProperty(key, property);

        try {
            FileOutputStream fos = new FileOutputStream(PROPERTIES_URL);
            properties.store(fos, "Configuration:");
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}